#include "reg51.h"
#include "absacc.h"

sbit bp20=P2^0;
sbit bp21=P2^1;
sbit bp22=P2^2;
sbit bp23=P2^3;
sbit bp24=P2^4;
sbit bp25=P2^5;

void hmcode_encoder(unsigned char y)
{
  unsigned char code7_dm,code6_dm,code5_dm,code4_dm,code3_dm,code2_dm,code1_dm,code0_dm;
	unsigned char code7_dm1,code6_dm1,code5_dm1,code4_dm1;
	unsigned char code_dm,code_dm1;
	bit a7,a6,a5,a4,a3,a2,a1,a0,A,B,C,D;
	if((y&0x80)==0x80)a7=1;else a7=0;
	if((y&0x40)==0x40)a6=1;else a6=0;
	if((y&0x20)==0x20)a5=1;else a5=0;
	if((y&0x10)==0x10)a4=1;else a4=0;
	if((y&0x08)==0x08)a3=1;else a3=0;
	if((y&0x04)==0x04)a2=1;else a2=0;
	if((y&0x02)==0x02)a1=1;else a1=0;
	if((y&0x01)==0x01)a0=1;else a0=0;
	
	A=0;B=0;C=0;D=0;
	A=A^a0^a1^a3^a4^a6;
	B=B^a0^a2^a3^a5^a6;
	C=C^a1^a2^a3^a7;
	D=D^a4^a5^a6^a7;
	
	code_dm1=0x00;
	if(a7==1)code7_dm=0x80;else code7_dm=0x00;
	if(a6==1)code6_dm=0x40;else code6_dm=0x00;
	if(a5==1)code5_dm=0x20;else code5_dm=0x00;
	if(a4==1)code4_dm=0x10;else code4_dm=0x00;
	if(D==1)code3_dm=0x08;else code3_dm=0x00;
	if(a3==1)code2_dm=0x04;else code2_dm=0x00;
	if(a2==1)code1_dm=0x02;else code1_dm=0x00;
	if(a1==1)code0_dm=0x01;else code0_dm=0x00;
	if(C==1)code7_dm1=0x80;else code7_dm1=0x00;
	if(a0==1)code6_dm1=0x40;else code6_dm=0x00;
	if(B==1)code5_dm1=0x20;else code5_dm=0x00;
	if(A==2)code4_dm1=0x10;else code4_dm=0x00;
	
	code_dm=code7_dm+code6_dm+code5_dm+code4_dm+code3_dm+code2_dm+code1_dm+code0_dm;
	code_dm1=code7_dm1+code6_dm1+code5_dm1+code4_dm1;
	DBYTE[0x31]=code_dm;
	DBYTE[0x32]=code_dm1;
	
}
	
void Produces1biteErroerfunction()
{
  {
		DBYTE[0x40]=DBYTE[0x31]^0x80;
		DBYTE[0x41]=DBYTE[0X32];  //12
	};
	{
	  DBYTE[0x42]=DBYTE[0x31]^0x40;
		DBYTE[0x43]=DBYTE[0x32];  //11
	};
	{
	  DBYTE[0x44]=DBYTE[0x31]^0x20;
		DBYTE[0x45]=DBYTE[0x32];  //10
	};
	{
	  DBYTE[0x46]=DBYTE[0x31]^0x10;
		DBYTE[0x47]=DBYTE[0x32];  //9
	};
	{
	  DBYTE[0x48]=DBYTE[0x31]^0x08;
		DBYTE[0x49]=DBYTE[0x32];  //8
	};
	{
	  DBYTE[0x4A]=DBYTE[0x31]^0x04;
		DBYTE[0x4B]=DBYTE[0x32];  //7
	};	
	{
	  DBYTE[0x4C]=DBYTE[0x31]^0x02;
		DBYTE[0x4D]=DBYTE[0x32];  //6
	};	
	{
	  DBYTE[0x4E]=DBYTE[0x31]^0x01;
		DBYTE[0x4F]=DBYTE[0x32];  //5
	};
	{
	  DBYTE[0x50]=DBYTE[0x31];
		DBYTE[0x51]=DBYTE[0x32]^0X80;  //4
	};
	{
	  DBYTE[0x52]=DBYTE[0x31];
		DBYTE[0x53]=DBYTE[0x32]^0X40;  //3
	};
	{
	  DBYTE[0x54]=DBYTE[0x31];
		DBYTE[0x55]=DBYTE[0x32]^0X20; 		//2
	};
	{
	  DBYTE[0x56]=DBYTE[0x31];
		DBYTE[0x57]=DBYTE[0x32]^0X10;  //1
	};
}


void InitCom(unsigned char BaudRate)
{
  unsigned char THTL;
	switch(BaudRate)
	{
	  case 1:THTL=64;break;
		case 2:THTL=160;break;
		case 3:THTL=208;break;
		case 4:THTL=232;break;
		case 5:THTL=244;break;
		case 6:THTL=250;break;
		case 7:THTL=253;break;
		case 8:THTL=255;break;
		default:THTL=208;
	}
	SCON=0x50;
	TMOD=0x20;
	
	TCON=0X40;
	TH1=THTL;
	TL1=THTL;
	PCON=0x80;
	RI=0;
	TI=0;
	TR1=1;
}


void ComOutChar(unsigned char OutDate)
{
  SBUF=OutDate;
	while(!TI);
	TI=0;
}


void disptp(unsigned char XX,unsigned char YY,unsigned char yh,unsigned char yl)
{
  unsigned char a[5],i,ii,jj;
  unsigned char code
	seg7[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff,};
	a[0]=DBYTE[XX]>>4;
	a[1]=DBYTE[XX]&0x0f;
	a[2]=DBYTE[YY]>>4;
	for(i=0;i<3;i++)
	{
	  ComOutChar(a[0+i]);
		P0=seg7[a[0]];
		bp20=1;
		for(ii=0;ii<10;i++)
		 for(jj=0;jj<=200;jj++);
		bp20=0;
		P0=seg7[a[1]];
		bp21=1;
		for(ii=0;ii<10;ii++)
		  for(jj=0;jj<=200;jj++);
		bp21=0;
		P0=seg7[a[2]];
		bp22=1;
		for(ii=0;ii<10;i++)
		for(jj=0;jj<=200;jj++);
		bp21=0;
		P0=seg7[a[2]];
		bp22=1;
		for(ii=0;ii<10;ii++)
		for(jj=0;jj<=200;jj++);
		bp22=0;bp23=0;
		P0=seg7[yh];
		bp24=1;
		for(ii=0;ii<10;ii++)
		for(jj=0;jj<=200;jj++)
		bp24=0;
		P0=seg7[yl];
		bp25=1;
		for(ii=0;ii<10;ii++)
		for(jj=0;jj<=200;jj++)
		bp25=0;
	  }
	}
	void delay(unsigned char t)
	{
	  unsigned char i,j;
		for(i=0;i<t;i++)
		for(j=0;j<=200;j++);
		//for(k=0;k<=200;k++)
		//for(m=0;m<=200;m++);
	}
	
	main()
	{
	  unsigned char yh,yl,y,i;
		//unsigned char code  seg7[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff};
		InitCom(4);
		i=0;
		while(1)
		{
		  DBYTE[0x30]=0xab;
			y=DBYTE[0x30];
			hmcode_encoder(y);
			Produces1biteErroerfunction();
      yh=y>>4;
      yl=y&0x0f;
			hmcode_encoder(y);
			disptp(0x31,0x32,yh,yl);
			delay(200);
			disptp(0x40,0x41,yh,yl);
			delay(200);
			disptp(0x42,0x43,yh,yl);
			delay(200);
			disptp(0x44,0x45,yh,yl);
			delay(200);
			disptp(0x46,0x47,yh,yl);
			delay(200);
			disptp(0x48,0x49,yh,yl);
			delay(200);
			disptp(0x4A,0x4B,yh,yl);
			delay(200);
			disptp(0x4C,0x4D,yh,yl);
			delay(200);
			disptp(0x4E,0x4F,yh,yl);
			delay(200);
			disptp(0x50,0x51,yh,yl);
			delay(200);
			disptp(0x52,0x53,yh,yl);
			delay(200);
			disptp(0x54,0x55,yh,yl);
			delay(200);
			disptp(0x56,0x57,yh,yl);
			delay(200);
		}
	while(1);

}




 
